<template>
  <div class="page">
    <h2>可下载的仓库报告</h2>
    
    <p class="description">
      请选择一个仓库并点击下载按钮，下载对应的报告文件。
    </p>

    <el-table :data="repositories" style="width: 100%" class="repository-table">
      <el-table-column prop="name" label="仓库名" width="200" />
      <el-table-column prop="link" label="仓库链接" />
      <el-table-column label="操作" width="150">
        <template v-slot="scope">
          <el-button
            type="primary"
            size="small"
            @click="downloadRepo(scope.row.link)"
          >下载报告</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, onMounted } from 'vue';
import { useDownloadStore } from '@/stores/downloadStore';  
import axios from 'axios';  // 引入 Axios 进行 API 调用

export default defineComponent({
  setup() {
    const downloadStore = useDownloadStore();  // 使用下载store
    const repositories = ref<{ name: string, link: string }[]>([]);  // 仓库列表

    // 获取仓库列表数据
    const fetchRepositories = async () => {
      try {
        const response = await axios.get('http://127.0.0.1:5000/getgitwarehouse');
        const urls = response.data.data;
        repositories.value = urls.map((url: string) => ({
          name: url.split('/').pop(),  // 提取仓库名
          link: url,
        }));
      } catch (error) {
        console.error('Failed to fetch repositories:', error);
      }
    };

    // 下载指定仓库报告
    const downloadRepo = (repoLink: string) => {
      const repoName = repoLink.split('/').pop();
      const downloadFileName = `${repoName}-report.md`;
      downloadStore.downloadFile(downloadFileName, repoLink);  // 调用下载store
    };

    // 在组件挂载时获取仓库列表
    onMounted(fetchRepositories);

    return {
      repositories,
      downloadRepo,
      downloadStore,
    };
  },
});
</script>

<style scoped>
.page {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  padding: 20px;
  background-color: #f5f5f5;
  border-radius: 8px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

h2 {
  margin-bottom: 20px;
  font-size: 1.8rem;
  color: #333;
}

.description {
  font-size: 1.2rem;
  color: #666;
  margin-bottom: 20px;
  text-align: center;
  max-width: 600px;
}

.repository-table {
  width: 100%;
  max-width: 800px;
}
</style>